//
//  KLMainViewController.m
//  used
//
//  Created by xalo on 16/7/20.
//  Copyright © 2016年 王凯. All rights reserved.
//

#import "KLMainViewController.h"

@interface KLMainViewController ()

/** 数据 */
@property (nonatomic, strong) NSArray *settings;
@property (nonatomic, strong) NSArray *settingImages;

@end

@implementation KLMainViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.tableView.backgroundColor = KLRGBColor(240, 240, 240);

    self.settings = @[@"我发布的", @"我卖出的", @"我买到的", @"我赞过的"];
    self.settingImages = @[@"fabu", @"maichu_副本", @"4", @"zan_副本"];
    
    [self.tableView registerNib:[UINib nibWithNibName:@"KLMainCell" bundle:nil] forCellReuseIdentifier:@"mainHeadCell"];
    
    [self.tableView registerNib:[UINib nibWithNibName:@"YSLSubCell" bundle:nil] forCellReuseIdentifier:@"YSLSubCell"];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
}

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    [self.tableView reloadData];
}


#pragma mark - <UITableViewDelegate>
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 3;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    if (section == 0) {
        return 1;
    }else if (section == 1) {
        return 4;
    }else {
        return 1;
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    
    if (indexPath.section == 0) {
        KLMainCell *cell = [tableView dequeueReusableCellWithIdentifier:@"mainHeadCell"];
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        BmobUser *user = [BmobUser getCurrentUser];
        if (user) {
            NSString *uname = [user objectForKey:@"uname"];
            if (uname.length) {
                
                cell.nameLabel.text = [user objectForKey:@"uname"];
            }else {
                cell.nameLabel.text = user.username;
            }
            
            BmobFile *icon = [user objectForKey:@"icon"];
            [cell.iconView sd_setImageWithURL:[NSURL URLWithString:icon.url] placeholderImage:[UIImage imageNamed:@"placehold_icon"]];
        }else {
            cell.nameLabel.text = @"注册/登录";
            cell.iconView.image = [UIImage imageNamed:@"placehold_icon"];
        }

        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.textLabel.font = [UIFont systemFontOfSize:15];
        cell.textLabel.textColor = [UIColor darkGrayColor];
  
        return cell;
    }else if (indexPath.section == 1) {
        
        YSLSubCell *cell = [tableView dequeueReusableCellWithIdentifier:@"YSLSubCell"];
        cell.name.text = self.settings[indexPath.row];
        cell.name.textColor = [UIColor darkGrayColor];
        cell.iconView.image = [UIImage imageNamed: self.settingImages[indexPath.row]];
//        cell.imageView.layer.cornerRadius =  cell.height/2;
//        cell.imageView.layer.masksToBounds = YES;
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        cell.selectionStyle = UITableViewCellSelectionStyleNone;

        
        //分割线
        UIView *line = [[UIView alloc] initWithFrame:CGRectMake(0, 50, self.view.width, 1)];
        line.backgroundColor = [[UIColor lightGrayColor] colorWithAlphaComponent:0.4];
        [cell addSubview:line];
        
        return cell;
    }else {
        YSLSubCell *cell = [tableView dequeueReusableCellWithIdentifier:@"YSLSubCell"];
        cell.name.text = @"设置";
        cell.name.textColor = [UIColor darkGrayColor];
        cell.iconView.image = [UIImage imageNamed:@"1.png"];
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        cell.selectionStyle = UITableViewCellSelectionStyleNone;

        return cell;
    }
    
}



#pragma mark - <UITableViewDelegate>
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 10;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kWidth, 20)];
    view.backgroundColor = self.view.backgroundColor;
    return view;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        return 100;
    }else {
        return 50;
    }
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    [self.tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    if (![BmobUser getCurrentUser]) {
        YSLLoginRegisterViewController *login = [[YSLLoginRegisterViewController alloc] init];
        [self presentViewController:login animated:YES completion:nil];
    }else {
        
        if (indexPath.section == 0) {
            
            KLMyMessageViewController *myVc = [[UIStoryboard storyboardWithName:@"KLMyMessageViewController" bundle:nil] instantiateInitialViewController];
            [self.navigationController pushViewController:myVc animated:YES];
            
            
        }else if (indexPath.section == 1 && indexPath.row == 0) {
            
            MyPublishViewController *PublishVC = [MyPublishViewController new];
            [self.navigationController pushViewController:PublishVC animated:YES];
            
        }else if (indexPath.section == 1 && indexPath.row == 1) {
            
            YSLMySellViewController *sellVC = [YSLMySellViewController new];
            [self.navigationController pushViewController:sellVC animated:YES];
            
        }else if (indexPath.section == 1 && indexPath.row == 2) {
            
            YSLMyBuyViewController *buyVC = [YSLMyBuyViewController new];
            [self.navigationController pushViewController:buyVC animated:YES];
            
        }else if (indexPath.section == 1 && indexPath.row == 3) {
            
            MyPraiseViewController *praiseVC = [MyPraiseViewController new];
            [self.navigationController pushViewController:praiseVC animated:YES];
            
        }else {
            
            MySetViewController *setVC = [MySetViewController new];
            [self.navigationController pushViewController:setVC animated:YES];
            
        }
    
    }
}









@end